<?php 
require_once(dirname(__FILE__).'/../../include/config.inc.php');
header('Content-type: application/json;charset=utf-8');


$dosql->Execute("SELECT `id` FROM `#@__order` WHERE  delstate='' ",6);
$count_all=$dosql->GetTotalRow(6); //所有工单

$dosql->Execute("SELECT `id` FROM `#@__order` WHERE `jindu`=1  AND delstate='' ",1);
$count1=$dosql->GetTotalRow(1); //等待受理


$dosql->Execute("SELECT `id` FROM `#@__order` WHERE `jindu`>1 and  `jindu`<5  AND delstate='' ",2);
$count2=$dosql->GetTotalRow(2); //受理中

$count=array('all'=>$count_all,'waiting'=>$count1,'doing'=>$count2,'working'=>99);

    //地图

$map=array();

// 签到 接单 数据
$sql = "SELECT f.* ,j.dataname as jinduname,o.truename as truename , o.address as address, o.school as school,o.mobile as mobile,o.post_time as post_time FROM `#@__order_log` as f
left join `#@__order`  as o on o.id=f.orderid  
left join `#@__cascadedata`  as j on j.datavalue=f.jindu  
WHERE  j.datagroup='jindu'  AND   o.delstate=''  AND  f.latitude!='' AND  f.jindu='4'  AND o.jindu>0  AND o.jindu<5  ";

$dosql->Execute($sql,9);
while($row2 = $dosql->GetArray(9))
{
    $map_item['latlngs']=$row2['latitude'].",".$row2['longitude'];
    $map_item['lat']=$row2['latitude'];
    $map_item['lng']=$row2['longitude'];
    if($row2['qiandao']=='true'){
        $row2['jindu']='qian';
    }
    if($row2['jiedan']=='true'){
        $row2['jindu']='jie';
    }
    $map_item['jindu']=$row2['jindu'];
    $map[]=$map_item;
}




// 工单列表
$sql = "SELECT f.*,t.dataname as typename,j.dataname as jinduname FROM `#@__order` as f
left join `#@__cascadedata`  as t on t.datavalue=f.apply_type  
left join `#@__cascadedata`  as j on j.datavalue=f.jindu  
WHERE t.datagroup='type'  AND j.datagroup='jindu'  AND f.jindu>0 AND f.delstate=''   order by f.id desc  limit 8   ";
$dosql->Execute($sql,8);
while($row = $dosql->GetArray(8))
{
   
    $m_truename="";
    if(!empty($row['manageid'])){
        $m_str=$row['manageid'];
        $m_arr=explode(",",$m_str);
        foreach($m_arr as $key=>$val){
            $m_truename.=getManage($val);
        }
    }


    $order_list[]=array(
        'area'=>$row['typename'],
        'renwu'=>getRepairMode($row['repairid']),
        'truename'=>$m_truename,
        'content'=>ReStrLen($row['content'],30),
        'jindu'=>$row['jinduname'],
        'id'=>$row['id']
    );


}

    //报修的设备类型

$sql = "SELECT count(o.id) as a,o.apply_device,t.dataname 
FROM `#@__order` as o 
left join   `#@__cascadedata` as t
on o.apply_device=t.datavalue
WHERE  t.datagroup='device' and o.delstate=''  group by o.apply_device ";
$dosql->Execute($sql,7);
$pie_count=$dosql->GetTotalRow(7);
while($order=$dosql->GetArray(7)){
    $typename=$order['dataname'];
    $cishu=$order['a'];
    $order_type[]=array('typename'=>$typename,'cnt'=>$cishu);
}


//当月完成工单数量
$managelist=array();
$dosql->Execute("SELECT *  FROM `#@__user`  WHERE level='3'   ",1);
while($user_arr=$dosql->GetArray(1)){
   $mid=$user_arr['id'];
   $m_name=starReplace($user_arr['truename']);
   $fee=getFeeThisMonth($mid);
   $sql_0 = "SELECT count(id) as a  FROM `#@__order`   WHERE   FIND_IN_SET('$mid',manageid)    AND  FROM_UNIXTIME(`post_time`,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m')  AND delstate=''    AND jindu!=0 ";
   $dosql->Execute($sql_0,2);
   while($order=$dosql->GetArray(2)){
    $cishu=$order['a'];
    }
    $managelist[]=array('truename'=>$m_name,'cishu'=>$cishu,'fee'=>$fee);
}
//对数组进行 排序
array_multisort(array_column($managelist,'fee'),SORT_DESC,$managelist);


// 工单的完成率
//1、 当天的派单数量
$sql_today_all = "SELECT count(id) as a  FROM `#@__order`  WHERE  FROM_UNIXTIME(`post_time`,'%Y%m%d')=DATE_FORMAT(CURDATE(),'%Y%m%d')  AND delstate=''  AND jindu!=0  ";
$dosql->Execute($sql_today_all,8);
while($order=$dosql->GetArray(8)){
    $cishu_today=$order['a'];
};
$sql_today_ok = "SELECT count(id) as a  FROM `#@__order`  WHERE  FROM_UNIXTIME(`post_time`,'%Y%m%d')=DATE_FORMAT(CURDATE(),'%Y%m%d')  AND delstate=''  AND jindu>'4'  ";
$dosql->Execute($sql_today_ok,9);
while($order=$dosql->GetArray(9)){
    $cishu_today_ok=$order['a'];
};
if($cishu_today_ok>0  ){
    $wanchenglv=99;
}else{
    $wanchenglv=0;
}



    //输出 json
    $errno=array('errno'=>0); //json状态为ok
    $json_arr=array('count'=>$count,'wanchenglv'=>$wanchenglv,'order'=>$order_list,'map'=>$map,'order_type'=>$order_type,'managelist'=>$managelist);
    $json_json=array_merge($errno,array('data'=>$json_arr));
    $data=json_encode($json_json,JSON_UNESCAPED_UNICODE);  
    echo $data;


    //获取工程师当月的积分
    function getFeeThisMonth($mid){
        //获取所有的 记录合计
       global $dosql;
        $all_fee="0";
        $sql_order="SELECT * FROM   `#@__fee_log`   WHERE manageid='$mid'  AND  FROM_UNIXTIME(`op_time`,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') ";
        $dosql->Execute($sql_order,7);
        while($order=$dosql->GetArray(7)){
            if($order['op']=='add'){
                $all_fee+=$order['fee'];
            }elseif($order['op']=='move'){
                $all_fee-=$order['fee'];
            }
        }
        return $all_fee;
    }

//获取 服务工程师
function getManage($mid){
    global $dosql;
    if($mid>0){
        $m=$dosql->GetOne("SELECT * FROM `#@__user` WHERE id='$mid' ORDER BY id desc");
        if(is_array($m) && isset($m['id']) ){
          return  starReplace($m['truename']);
         }
    }
    
}



function starReplace($name, $num = 0)
{
    if ($num && mb_strlen($name, 'UTF-8') > $num) {
        return mb_substr($name, 0, 4) . '*';
    }
 
    if ($num && mb_strlen($name, 'UTF-8') <= $num) {
        return $name;
    }
 
    $doubleSurname = [
        '欧阳', '太史', '端木', '上官', '司马', '东方', '独孤', '南宫',
        '万俟', '闻人', '夏侯', '诸葛', '尉迟', '公羊', '赫连', '澹台', '皇甫', '宗政', '濮阳',
        '公冶', '太叔', '申屠', '公孙', '慕容', '仲孙', '钟离', '长孙', '宇文', '司徒', '鲜于',
        '司空', '闾丘', '子车', '亓官', '司寇', '巫马', '公西', '颛孙', '壤驷', '公良', '漆雕', '乐正',
        '宰父', '谷梁', '拓跋', '夹谷', '轩辕', '令狐', '段干', '百里', '呼延', '东郭', '南门', '羊舌',
        '微生', '公户', '公玉', '公仪', '梁丘', '公仲', '公上', '公门', '公山', '公坚', '左丘', '公伯',
        '西门', '公祖', '第五', '公乘', '贯丘', '公皙', '南荣', '东里', '东宫', '仲长', '子书', '子桑',
        '即墨', '达奚', '褚师', '吴铭'
    ];
 
    $surname = mb_substr($name, 0, 2);
    if (in_array($surname, $doubleSurname)) {
        $name = mb_substr($name, 0, 2) . str_repeat('*', (mb_strlen($name, 'UTF-8') - 2));
    } else {
        $name = mb_substr($name, 0, 1) . str_repeat('*', (mb_strlen($name, 'UTF-8') - 1));
    }
 
 
    return $name;
}

//获取任务类型
function getRepairMode($mid){
    global $dosql;
    $renwu="";
    if($mid>0){
        $m=$dosql->GetOne("SELECT * FROM `#@__cascadedata` WHERE   datagroup='renwu' AND  datavalue='$mid'  ORDER BY id desc");
        if(is_array($m) && isset($m['id']) ){
            $renwu=$m['dataname'];
        }
        return $renwu;
    }
    
}

    ?>

